package algo;

import java.util.*;

public class DisplayTableOfFoodOrdersInRestaurant_1418 {
    public List<List<String>> displayTable(List<List<String>> orders) {
        List<List<String>> ans = new ArrayList<>();
        TreeSet<String> foodItem = new TreeSet<>();
        Map<Integer, Map<String, Integer>> tableFoods = new HashMap<>();
        for (List<String> order : orders) {
            int table = Integer.parseInt(order.get(1));
            String food = order.get(2);
            foodItem.add(food);
            Map<String, Integer> tableMap = tableFoods.getOrDefault(table, new HashMap<>());
            int curr = tableMap.getOrDefault(food, 0);
            curr++;
            tableMap.put(food, curr);
            tableFoods.put(table, tableMap);
        }
        int index = 1;
        Map<String, Integer> foodIndex = new HashMap<>();
        List<String> header = new ArrayList<>();
        header.add("Table");
        for (String foodName : foodItem) {
            foodIndex.put(foodName, index++);
            header.add(foodName);
        }
        ans.add(header);

        List<Integer> tableIndex = new ArrayList<>(tableFoods.keySet());
        Collections.sort(tableIndex);
        for (int i : tableIndex) {
            List<String> res = new ArrayList<>();
            res.add(String.valueOf(i));
            for (int j = 1; j < index; j++) {
                res.add("0");
            }
            for (Map.Entry<String, Integer> entry : tableFoods.get(i).entrySet()) {
                res.add(foodIndex.get(entry.getKey()), String.valueOf(entry.getValue()));
            }
            ans.add(res);
        }
        return ans;
    }

    public static void main(String[] args) {
        TreeSet<String> foodItem = new TreeSet<>();
        foodItem.add("ac");
        foodItem.add("ae");
        System.out.println(foodItem);
        int i = 0;
        for (String s : foodItem) {
            System.out.println(s + " " + i++);
        }
    }
}
